from rest_framework import serializers
from .models import Student

# class StudentSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Student
#         fields = '__all__'
#

class StudentSerializer(serializers.ModelSerializer):
    gender_display = serializers.CharField(
        source='get_gender_display',
        read_only=True,
        label="性别显示"
    )

    class Meta:
        model = Student
        fields = ['id', 'name', 'age', 'gender', 'gender_display', 'email', 'phone']
        extra_kwargs = {
            'gender': {'write_only': True},
            'email': {'required': True}
        }

    def validate_phone(self, value):
        if not value.isdigit() or len(value) != 11:
            raise serializers.ValidationError("手机号必须为11位数字")
        return value